package com.efivestar.idm.api.util;

import com.efivestar.idm.api.common.Cons;
import com.efivestar.idm.api.service.dto.PowerDto;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class ByteUtils {

    /**
     * 将十六进制的数据转换成字节数组.
     * @param str
     * @return
     */
    public static byte[] hexToBytes(String str) {
        if(str == null || str.trim().equals("")) {
            return new byte[0];
        }

        byte[] bytes = new byte[str.length() / 2];
        for(int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }

        return bytes;
    }

    /**
     * 将字节数组转换成十六进制的数据.
     * @param bytes
     * @return
     */
    public static String bytes2hex(byte ...bytes) {
        final String HEX = "0123456789abcdef";
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) {
            // 取出这个字节的高4位，然后与0x0f与运算，得到一个0-15之间的数据，通过HEX.charAt(0-15)即为16进制数
            sb.append(HEX.charAt((b >> 4) & 0x0f));
            // 取出这个字节的低位，与0x0f与运算，得到一个0-15之间的数据，通过HEX.charAt(0-15)即为16进制数
            sb.append(HEX.charAt(b & 0x0f));
        }
        return sb.toString();
    }


    /**
     * 字节数组拼接.
     * @param bts
     * @return
     */
    public static byte[] byteArrayConcat(byte[] ...bts){
        int totalLength = 0;
        for (byte[] bt : bts) {
            totalLength += bt.length;
        }
        byte result[] = new byte[totalLength];
        int copyStartIndex = 0;
        for (byte[] bt : bts) {
            System.arraycopy(bt, 0, result, copyStartIndex, bt.length);
            copyStartIndex += bt.length;
        }
        return result;
    }

    /**
     * 将int数值写入2个字节的字节数组.
     * @param number
     * @return
     */
    public static byte[] intToBytes(int number){
        byte bts[] = new byte[2];
        ByteBuf numBBF = Unpooled.copiedBuffer(bts);
        numBBF.writerIndex(0);
        numBBF.writeShort(number);
        numBBF.getBytes(0, bts);
        return bts;
    }

    /**
     * 截取字节数组.
     * @param src 字节数组.
     * @param begin 开始位置.
     * @param count 截取长度.
     * @return
     */
    public static byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        System.arraycopy(src, begin, bs, 0, count);
        return bs;
    }


    /**
     * 从字节中获取电量信息.
     * @param power
     * @return
     */
    public static PowerDto getPower(byte power) {
        //默认先设置成正常电压
        PowerDto.PowerDtoBuilder builder = PowerDto.builder().powerLevel(Cons.COMMON_POWER);
        int i = Integer.parseInt(bytes2hex(power), 16);
        //超过1237代表，高位为1，此时是低电压
        if (i > 127) {
            builder.powerLevel(Cons.LOWER_POWER);
            i = i - 128;
        }
        builder.powerPercent(i);
        return builder.build();
    }


    /**
     * 解析用户的编码.
     * @param codeBf
     * @return
     */
    public static String getUserCode( ByteBuf codeBf) {
        String userCode = "";
        int sum = 0;
        for (int i = 0; i < 2; i++) {
            sum += codeBf.readUnsignedByte();
        }
        if (sum < 10) {
            userCode = "00" + sum;
        } else {
            userCode = "0" + sum;
        }
        return userCode;
    }

    public static void main(String[] args) {
        int i = Integer.parseInt("FF", 16);
        if (i > 127) {
            i = 127 - i;
        }
        System.out.println( i);
    }
}
